<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function() {
	resetForm();
	
	$('#findFaculty').keyup(function() {
		searchFaculty();
	});
	
	$(document).on('click', '.faculty', function() {
		resetForm();
		$('#command').val('updateFaculty');
		$('#faculty_no').val($(this).attr('name'));
		$('#faculty_name').val($(this).find('.faculty_name').text());
		$('#submitBtn').html('수정');
		$('#resetBtn').show();
		$('#delBtn').show();
		$('#findFaculty').val('');
		$('#facultylist *').remove();
	});
	
	$('#submitBtn').click(function() {
		$('#editForm').submit();
	});
	
	$('#delBtn').click(function() {
		location.href="major.do?command=deleteFaculty&&faculty_no="+$('#faculty_no').val();
	});
	
	$('#resetBtn').click(function() {
		resetForm();
	});
});

function searchFaculty() {
	facultyhtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getFacultyList&&search="+$('#findFaculty').val(),
		dataType: "json",
		
		success:function(data){			
			var list=data.facultylist;
			for (var i=0;i<list.length;i++) {
				facultyhtml+="<div class='faculty' name='"+list[i].faculty_no+"'><div class='faculty_name'>"+list[i].faculty_name+"</div></div>";
			}
			$('#facultylist').html(facultyhtml);
		}
	});
}

function resetForm() {
	$('#command').val('registerFaculty');
	$('#faculty_no').val('0');
	$('#faculty_name').val('');
	$('#submitBtn').html('등록');
	$('#resetBtn').hide();
	$('#delBtn').hide();
}
</script>
</head>
<body>
<a href="index.do?command=menu">메뉴로</a>
<div id="findDiv">
	<input id="findFaculty">
	<div id="facultylist"></div>
</div>
<div id="editDiv">
	<div id="formDiv">
		<form action="major.do" method="post" id="editForm">
			<input type="hidden" id="command" name="command" value="registerFaculty">
			<input type="hidden" id="faculty_no" name="faculty_no" value="0">
			학부 이름 <input id="faculty_name" name="faculty_name">
		</form>
		<div id="submitBtn">등록</div>
		<div id="delBtn">삭제</div>
		<div id="resetBtn">새 학부 등록</div>
	</div>
</div>
</body>
</html>